<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#include "../common/link.ftl"/>
    <style>
        .error{
            color: red;
        }
    </style>
    <script>
        $(function () {
            //在上面赋值,下面赋值的话,加载的时候   浏览器拿到的是空的
            $('#admin').prop('checked', ${(employee?? && employee.admin)?c});
            var ids = [];
            $.each($('.selfRoles > option'), function (index, domele) {
                // console.log(arguments)
                ids.push($(domele).val());
            });
            $.each($('.allRoles > option'), function (index, domele) {
                //console.log($.inArray(domele,ids));
                if ($.inArray($(domele).val(), ids) >= 0) {
                    $(domele).remove();
                }
            });

            console.log($('#admin').prop('checked'));
            console.log(${(employee?? && employee.admin)?c});


            var roleDiv;
            $('#admin').change(function () {
                // console.log(1);
                // console.log(this.checked);
                if (this.checked) {
                    roleDiv = $('#role').detach();
                } else {
                    //$('#adminEle').after(roleDiv);
                    $('#admin').closest('.form-group').after(roleDiv);
                }
            });
            if ($('#admin').prop('checked')) {
                roleDiv = $('#role').detach();
            }
            <#--<%--            <c:if test="${employee.admin}">--%>-->
            <#--<%--                roleDiv = $('#role').detach();--%>-->
            <#--<%--            </c:if>--%>-->


            $('#submitBtn').click(function () {
                $('.selfRoles > option').prop('selected', true);
                $("#editForm").submit();
                // $('#editForm').ajaxSubmit(function (data) {
                //     if(data.success){
                //         //window.location.reload();
                //         $.messager.alert("温馨提示","操作成功,两秒后更新页面");
                //         setTimeout(function () {
                //             window.location.href="/employee/list.do";
                //         },2000);
                //     }else{
                //         //alert(data.msg);
                //         $.messager.alert("温馨提示",data.msg);
                //     }
                // })
            });
        });

        function moveAll(srcClass, targetClass) {
            $('.' + srcClass + '>option').appendTo($('.' + targetClass));
        }

        function moveSelected(srcClass, targetClass) {
            $('.' + srcClass + '>option:selected').appendTo($('.' + targetClass));
        }
        $(function () {
            $('#editForm').validate({
                rules:{
                    name:{
                        required:true,
                        minlength:2
                    },
                    password:{
                        required: true,
                        rangelength:[6,16]
                    },
                    repassword:{
                        required:true,
                        equalTo:'#password'
                    },
                    email:{
                        required:true,
                        email: true
                    },
                    age:{
                        required:true,
                        range:[18,65]
                    }
                },
                messages:{
                    name:{
                        required:'用户名必填',
                        minlength:'名字长度不能少于2位'
                    },
                    password:{
                        required: '密码必填',
                        rangelength:'密码长度为6-16位之间'
                    },
                    repassword:{
                        required:'验证密码必填',
                        equalTo:'两次密码不一致'
                    },
                    email:{
                        required:'电子邮箱必填',
                        email: '邮箱格式错误'
                    },
                    age:{
                        required:'年龄必填',
                        range:'年龄需要在18-65之间'
                    }
                }
            });
        });
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl"/>
    <!--菜单回显-->
    <#assign currentMenu="employee" />
    <#include "../common/menu.ftl"/>
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/employee/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="${(employee.id)!}" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(employee.name)!}"
                                   placeholder="请输入用户名">
                        </div>
                    </div>
                    <#--    如果存在,就是true   否则就是false     -->
                    <#if !employee??>
                        <div class="form-group">
                            <label for="password" class="col-sm-2 control-label">密码：</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control" id="password" name="password"
                                       placeholder="请输入密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="repassword" class="col-sm-2 control-label">验证密码：</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control" id="repassword" name="repassword"
                                       placeholder="再输入一遍密码">
                            </div>
                        </div>
                    </#if>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">电子邮箱：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="email" name="email" value="${(employee.email)!}"
                                   placeholder="请输入邮箱">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="age" name="age" value="${(employee.age)!}"
                                   placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="dept" class="col-sm-2 control-label">部门：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="dept" name="dept.id">
                                <#list depts as d>
                                    <option value="${d.id}"
                                            ${((employee.dept.id)?? && (employee.dept.id) == d.id)?string('selected','')}>
                                        ${d.name}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" id="adminEle">
                        <label for="admin" class="col-sm-2 control-label">超级管理员：</label>
                        <div class="col-sm-6" style="margin-left: 15px;">
                            <input type="checkbox" id="admin" name="admin" class="checkbox">
                            <#--<%--                            <c:if test="${employee.admin}">--%>-->
                            <#--<%--                                <script>--%>-->
                            <#--<%--                                    $('#admin').prop('checked',true);--%>-->
                            <#--<%--                                </script>--%>-->
                            <#--<%--                            </c:if>--%>-->
                            <#--<%--                            <script>--%>-->
                            <#--<%--                                $(function () {--%>-->
                            <#--<%--                                   --%>-->
                            <#--<%--                                });--%>-->
                            <#--<%--                            </script>--%>-->
                        </div>
                    </div>
                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配角色：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allRoles" size="15">
                                    <#list roles as role>
                                        <option value="${role.id}">${role.name}</option>
                                    </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary  " style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allRoles', 'selfRoles')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfRoles', 'allRoles')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allRoles', 'selfRoles')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfRoles', 'allRoles')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>

                            <div class="col-sm-2">
                                <select multiple class="form-control selfRoles" size="15" name="ids">
<#--                                    <#if employee??>-->
                                        <#list (employee.roles)! as role>
                                            <option value="${role.id}">${role.name}</option>
                                        </#list>
<#--                                    </#if>-->
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="submitBtn" type="button" class="btn btn-primary">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>

                </form>

            </div>
        </section>
    </div>
    <#include "../common/footer.ftl"/>
</div>
</body>
</html>
